package com.ethannhu.todaynews.util

import com.google.gson.FieldNamingPolicy
import com.google.gson.Gson
import com.google.gson.GsonBuilder


/**
 * JSON工具类
 */
object JSONUtil {
    fun createGson(): Gson {
        val gsonBuilder = GsonBuilder()

        //驼峰转下划线
        gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
        return gsonBuilder.create()
    }
    /**
     * 转为 JSON字符串
     */
    fun toJSON(data: Any?): String {
        return createGson().toJson(data)
    }

    /**
     * 将 JSON转为对象
     */
    fun <T> fromJSON(data: String?, clazz: Class<T>?): T {
        return createGson().fromJson(data, clazz)
    }
}